package com.google.code.swingonfire.utility.date;

import java.text.DateFormatSymbols;
import java.util.Calendar;
import java.util.Locale;

public abstract class AbstractDateRenderInfoPart implements DateRenderInfoPart
{

    private final int length;

    public AbstractDateRenderInfoPart(int length)
    {
        super();

        this.length = length;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void format(StringBuilder builder, Calendar calendar, DateFormatSymbols dateFormatSymbols, Locale locale)
    {
        builder.append(getValue(calendar, dateFormatSymbols, locale));
    }

    protected String formatNumber(int value)
    {
        String result = formatNumber(String.valueOf(Math.abs(value)));

        if (value < 0)
        {
            result = "-" + result;
        }

        return result;
    }

    protected String formatNumber(String value)
    {
        while (value.length() < length)
        {
            value = "0" + value;
        }

        return value;
    }

    protected abstract String getValue(Calendar calendar, DateFormatSymbols dateFormatSymbols, Locale locale);

}
